package com.atguigu.gulimall.sso.controller;

import com.atguigu.gulimall.sso.model.Employee;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HelloController {

    @Value("${sso.server.url}")
    String ssoUrl;

    //无需登录即可访问
    @GetMapping("/hello")
    @ResponseBody
    public String hello() {
        return "hello";
    }

    /**
     * @param model
     * @param session
     * @param token   只要去ssoserver登录成功了，跳回来就会带上
     * @return
     */
    @GetMapping("/employess")
    public String employess(Model model, HttpSession session, @RequestParam(value = "token", required = false) String token) {
        //感知是从ssorserver鞥路成功调回来的
        if (!StringUtils.isEmpty(token)){
            //跳回来带上的，登录成功了
            session.setAttribute("loginUser","zhangsan");
        }
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser == null) {
            return "redirect:" + ssoUrl + "?redirect_url=http://client1.com:40001/employess";
        } else {
            List<String> emps = new ArrayList<>();
            emps.add("张三");
            emps.add("李四");
            emps.add("王五");
            model.addAttribute("emps", emps);
            return "list";
        }
    }
}
